package com.Login.web;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

/**
 * Created by XingYang on 2019/2/28.
 */
public class ShiroTest {
   private static final Logger log = Logger.getLogger(ShiroTest.class);

    public static void main(String[] args){

        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        Subject currentUser = SecurityUtils.getSubject();
        if (!currentUser.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken("zhang","123");
            token.setRememberMe(true);

            try{
                currentUser.login(token);
            }catch (UnknownAccountException e){
                log.info("该用户不存在！");
                return;
            }catch (IncorrectCredentialsException e){
                log.info("用户名或密码错误！");
                return;
            }

        }
        System.out.println(currentUser.getPrincipal());
        System.exit(0);


    }
}
